<template>
  <div>
    <div v-if="!showEdit" class="container">
      <div class="header">
        <div class="title">房间列表</div>
        <el-button style="margin-left:30px;" @click.prevent="addRoom"
                   type="primary" plain size="medium">添加房间</el-button>
        <div style="margin-left:30px;">
          <lin-search @query="onQueryChange" ref="searchKeyword"/>
        </div>
      </div>
      <transition name="fade">
        <div class="search" v-if="keyword">
          <p class="search-tip">
            搜索“<span class="search-keyword">{{ keyword }}</span
          >”， 找到 <span class="search-num">{{ totalNums }}</span> 条房间信息
          </p>
          <button class="search-back" @click="backInit">返回全部房间</button>
        </div>
      </transition>
      <el-table stripe v-loading="loading" :data="tableData">
<!--        <el-table-column fixed="left" prop="id" label="id" width="100"></el-table-column>-->
<!--        <el-table-column prop="img" width="100" label="图片">-->
<!--          <template v-if="scope.row.img" slot-scope="scope">-->
<!--            <el-image :src="scope.row.img" :preview-src-list="imgSrcList" style="max-width: 200px;"> </el-image>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column :show-overflow-tooltip="true" prop="title" label="标题" width="200"></el-table-column>-->
<!--        <el-table-column :show-overflow-tooltip="true" prop="subtitle" label="副标题" width="250"></el-table-column>-->
        <el-table-column prop="name" label="房间名字" width="300"></el-table-column>
        <el-table-column prop="belong_building" label="房间所在楼栋" width="200"></el-table-column>
        <el-table-column prop="floor" label="房间所在楼层" width="300"></el-table-column>
        <el-table-column prop="manager" label="房间管理员" width="200"></el-table-column>
        <el-table-column prop="phone" label="联系方式" width="300"></el-table-column>
<!--        <el-table-column prop="manager" label="管理员" width="150">-->
<!--          <template v-if="scope.row.online" slot-scope="scope">{{ scope.row.online | onLine }}</template>-->
<!--        </el-table-column>-->
        <el-table-column width="300" fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button @click.prevent="handleEdit(scope.row)" type="primary" plain size="mini">编辑</el-button>
            <el-button
              v-permission="{ permission: ['删除房间'], type: 'disabled' }"
              @click.prevent="handleDelete(scope.row)"
              type="danger"
              size="mini"
              plain
            >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          @current-change="handleCurrentChange"
          :background="true"
          :page-size="pageCount"
          :current-page="currentPage"
          v-if="refreshPagination"
          layout="prev, pager, next, jumper"
          :total="totalNums"
        ></el-pagination>
      </div>
    </div>
    <room-edit v-else @editClose="editClose" :room-id="roomId" :isCreate="isCreate"></room-edit>
  </div>
</template>

<script>
import Building from '@/model/ag_building'
import Room from '@/model/ag_room'
import RoomEdit from './room-edit'
import LinSearch from '@/component/base/search/lin-search'


export default {
  components: { RoomEdit,LinSearch },
  data() {
    return {
      roomId: '',
      isCreate: false,
      tableData: [],
      showEdit: false,
      totalNums: 0,
      currentPage: 1,
      pageCount: 10,
      refreshPagination: true,
      //搜索关键词
      keyword: null,
      searchKeyword: '',
      buildings:'',
      roomBuilding:{
        buildingId:'',
        roomName:'',
      },
    }
  },
  watch: {
    // 关键字搜索
    searchKeyword(newVal) {
      if (newVal) {
        this.keyword = newVal
        if (this.searchUser) {
          this.keyword = `${newVal}`
        }
      } else {
        this.keyword = ''
        this.$refs.searchKeyword.clear()
      }
      this.searchPage()
    },
  },

  async created() {
    this.loading = true
    this.getBuildings()
    this.getRooms()
    this.loading = false
  },
  methods: {
    async getRooms() {
      const page = this.currentPage - 1
      const count = this.pageCount
      const rooms = await Room.getRooms(page, count)
      this.tableData= rooms.items
      this.totalNums = rooms.total
    },
    async searchPage() {
      const page = this.currentPage - 1
      const count = this.pageCount
      const rooms = await Room.search({
        keyword: this.searchKeyword,
        page,
        count,
      })
      this.tableData = rooms.items
      this.totalNums = rooms.total
    },

    async getBuildings(){
      this.buildings = await Building.getBuildings();
    },

    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this.getRooms()
      this.loading = false
    },
    handleEdit(val) {
      this.isCreate = false
      this.roomId = `${val.id}`
      this.showEdit = true
    },
    handleDelete(val) {
      this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await Room.deleteRoom(val.id)
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.getRooms()
          this.$message({
            type: 'success',
            message: `${res.message}`,
          })
        }
      })
    },
    addRoom() {
      this.isCreate = true
      this.roomId = null
      this.showEdit = true
    },
    editClose() {
      this.showEdit = false
      this.getRooms()
    },
    // 清空检索
    async backInit() {
      this.searchKeyword = ''
      this.keyword = ''
      this.tableData = []
      this.loading = true
      this.getRooms()
      this.loading = false
    },
    async onQueryChange(query) {
      this.searchKeyword = query.trim()
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    // justify-content: space-between;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
  .search {
    height: 52px;
    width: 100%;
    background: rgba(57, 99, 188, 0.1);
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-top: 24px;
    margin-bottom: 24px;

    .search-tip {
      margin-left: 40px;
      height: 52px;
      line-height: 52px;
      color: #354058;
      font-size: 14px;

      .search-keyword {
        color: $theme;
      }
      .search-num {
        color: #f4516c;
      }
    }
    .search-back {
      margin: 8px 20px;
      height: 32px;
      background: #f4516c;
      border: none;
      border-radius: 2px;
      color: #fff;
      padding: 0 13px;
      font-size: 14px;
      cursor: pointer;
    }
  }
}

.long-text-hidden {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
</style>
